﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmTimKiemUyNhiemChi : Form
    {
        private UyNhiemChiManagement uynhiemchiMgt = new UyNhiemChiManagement();
        private NganHangDoiTacManagement nganhangdoitacMgt = new NganHangDoiTacManagement();
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public class UNC
        {
            public string MaSo { get; set; }
            public DateTime NgayThang { get; set; }
            public Int64 SoTien { get; set; }
            public string TenNganHang { get; set; }
            public string DoiTac { get; set; }
        }

        public FrmTimKiemUyNhiemChi()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            var lstUyNhiemChi = (from u in GetAllUyNhiemChi()
                                 join n in GetAllNganHangDoiTac() on u.IdNganHangNhan equals n.Id into un
                                 from x in un.DefaultIfEmpty()
                                 join p in GetAllNhaCungCap() on u.IdNhaCungCap equals p.Id into up
                                 from y in up.DefaultIfEmpty()
                                 where u.NgayThang.Date >= dtTuNgay.Date && u.NgayThang.Date <= dtDenNgay.Date ||
                                    u.MaSo == txtTimKiem.Text || u.NguoiLap.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                    y.TenNhaCungCap.ToLower().Contains(txtTimKiem.Text.ToLower())
                                 select new UNC() { MaSo = u.MaSo, NgayThang = u.NgayThang, SoTien = u.SoTien, TenNganHang = x.Ten, DoiTac = y.TenNhaCungCap }).ToList(); 

            dgvDanhSachUyNhiemChi.AutoGenerateColumns = false;
            dgvDanhSachUyNhiemChi.DataSource = lstUyNhiemChi;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            FrmUyNhiemChi frmUyNhiemChi = new FrmUyNhiemChi(this.nhanvien);
            this.Hide();
            frmUyNhiemChi.ShowDialog();
            this.Show();
        }

        private void dgvDanhSachUyNhiemChi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string maso = dgvDanhSachUyNhiemChi.CurrentRow.Cells[0].Value.ToString();
            UyNhiemChi uynhiemchi = (from u in GetAllUyNhiemChi()
                                     where u.MaSo == maso
                                     select u).FirstOrDefault();

            if (uynhiemchi != null)
            {
                FrmUyNhiemChi frmUyNhiemChi = new FrmUyNhiemChi(uynhiemchi, this.nhanvien);
                this.Hide();
                frmUyNhiemChi.ShowDialog();
                this.Show();
                btnTimKiem_Click(null, null);
            }
        }

        public List<UyNhiemChi> GetAllUyNhiemChi()
        {
            ResultObj result = new ResultObj();
            result = uynhiemchiMgt.GetAllUyNhiemChi();
            List<UyNhiemChi> lstUyNhiemChi = (List<UyNhiemChi>)result.Result;
            return lstUyNhiemChi;
        }

        public List<NganHangDoiTac> GetAllNganHangDoiTac()
        {
            ResultObj result = new ResultObj();
            result = nganhangdoitacMgt.GetAllNganHangDoiTac();
            List<NganHangDoiTac> lstNganHangDoiTac = (List<NganHangDoiTac>)result.Result;
            return lstNganHangDoiTac;
        }

        public List<NhaCungCap> GetAllNhaCungCap()
        {
            ResultObj result = new ResultObj();
            result = nhacungcapMgt.GetAllNhaCungCap();
            List<NhaCungCap> lstNhaCungCap = (List<NhaCungCap>)result.Result;
            return lstNhaCungCap;
        }
    }
}
